Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
The auto-bind npm package is used to automatically bind methods of a class to their instance. This is particularly useful in React components to avoid having to manually bind 'this' in the constructor for every method that needs it.
Automatic binding of class methods
This feature automatically binds all methods of a class instance to the instance itself, ensuring that 'this' within the methods refers to the class instance even when the method is extracted and called separately.
const autoBind = require('auto-bind');
class MyClass {
constructor() {
autoBind(this);
}
myMethod() {
// method body that uses 'this'
}
}
const instance = new MyClass();
const { myMethod } = instance;
myMethod(); // 'this' is correctly bound to the instance
bind-methods is a package that binds an object's methods to itself. It is similar to auto-bind but does not automatically exclude constructor, React lifecycle methods, or methods already bound. It requires you to specify which methods to bind.
class-autobind is another package that offers automatic binding of class methods to the instance. It is similar to auto-bind but is implemented as a decorator, which can be used in projects that support JavaScript decorators.
react-autobind is designed specifically for React components to automatically bind methods to the component instance. It is similar to auto-bind but is tailored for React and can be more convenient for React developers.
Automatically bind methods to their class instance
$ npm install --save auto-bind
const autoBind = require('auto-bind');
class Unicorn {
constructor(name) {
this.name = name;
autoBind(this);
}
message() {
return `${this.name} is awesome!`;
}
}
const unicorn = new Unicorn('Rainbow');
// grab the method off the class instance
const message = unicorn.message;
// still bound to the class instance
message();
//=> 'Rainbow is awesome!'
// without `autoBind(this)`, the above would have resulted in
message();
//=> Error: Cannot read property 'name' of undefined
MIT © Sindre Sorhus
FAQs
Automatically bind methods to their class instance
We found that auto-bind demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.